package cn.itcast.java.thread;

/**
 * Author itcast
 * Date 2021/10/14 6:47
 * Desc TODO
 */
public class Ticket implements Runnable{

    //卖票
    private int ticket = 100;
    //锁对象
    Object lock = new Object();

    /**
     *执行每个窗口卖票
     */
    @Override
    public void run() {
        while(true){
            synchronized (lock) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "正在卖：" + ticket--);
                }
            }
        }
    }
}
